﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Attacker
{
	public partial class Form1 : Form
	{
		string connectionString = "server=.;database=Watermarking;integrated security=SSPI;";
        private Database_mag data_mag;
		progressForm pro;
        string privateKey;
        int rowMod;
        int colMod;
        int bitMod;

        public Form1()
		{
			InitializeComponent();
			this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this)));
			this.skinEngine1.SkinFile = Application.StartupPath + "//DeepCyan.ssk";
			GetTables();
            data_mag = new Database_mag();
			comboBox1.Text = "weather";
		}


        private void GetTables()
		{
			SqlConnection con = new SqlConnection(connectionString);
			con.Open();
			DataTable dt = con.GetSchema("Tables");
			con.Close();
			foreach (DataRow row in dt.Rows)
				comboBox1.Items.Add(row[2].ToString());
		}

        private void button2_Click(object sender, EventArgs e)//连接数据库
        {
            data_mag.Connection();
            button2.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e) //比特反转攻击：输入参数 __% 随机选取行 更改所有可改列的所有可改bit
		{
			if (data_mag.tag == 0)
			{
				MessageBox.Show("请先连接数据库");
				return;
			}
			if (textBox1.Text == "" || textBox2.Text == "" || numericUpDown1.Text == "" || textBox4.Text == "")
			{
				MessageBox.Show("请完善必要参数");
				return;
			}
			button1.Enabled = false;
			rowMod = Convert.ToInt32(numericUpDown1.Text);
			colMod = Convert.ToInt32(textBox1.Text);
			bitMod = Convert.ToInt32(textBox2.Text);
			Thread th = new Thread(BitAtt);
			th.IsBackground = true;
			th.Start();
			pro = new progressForm();
			pro.StartPosition = FormStartPosition.CenterScreen;
			pro.Show();
			button1.Enabled = true;
		}
		private void BitAtt()
		{
			Bit_reverse attack = new Bit_reverse();
			attack.bit_attack(data_mag, colMod, bitMod, rowMod);
			MessageBox.Show("攻击完成");
			ClosePro();
		}

        private void button3_Click(object sender, EventArgs e)
        {
            if (data_mag.tag == 0)
            {
                MessageBox.Show("请先连接数据库");
                return;
            }
            if (textBox1.Text == "" || textBox2.Text == "" || numericUpDown1.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("请完善必要参数");
                return;
            }
            button3.Enabled = false;
            rowMod = Convert.ToInt32(numericUpDown1.Text);
            colMod = Convert.ToInt32(textBox1.Text);
            bitMod = Convert.ToInt32(textBox2.Text);
            privateKey = textBox4.Text;
			Thread th = new Thread(MarkAtt);
			th.IsBackground = true;
			th.Start();
			pro = new progressForm();
			pro.StartPosition = FormStartPosition.CenterScreen;
			pro.Show();
			button3.Enabled = true;
		}
		private void MarkAtt()
		{
			Mark cc = new Mark();
			cc.Mark_Click(data_mag, privateKey, rowMod, colMod, bitMod);
			ClosePro();
		}

        private void button4_Click(object sender, EventArgs e)
        {
            data_mag.Revert();
            MessageBox.Show("恢复完成");
        }
		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (data_mag.tag != 0)
				data_mag.Revert();
		}
		delegate void ProDele();
		private void ClosePro()
		{
			if (pro.InvokeRequired)
			{
				ProDele d = new ProDele(ClosePro);
				pro.Invoke(d);
			}
			else
				pro.Close();
		}

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
